/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package DAO;

import DTO.ChuyenTauDTO;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;

/**
 *
 * @author Nobita Chau
 */
public class ChuyenTauDAO {
    
    public static boolean Them(ChuyenTauDTO n) {
        try {
            Connection conn = DBConnect.Connect();
            Statement statement = conn.createStatement();
            String sql = String.format ("INSERT INTO "
                    + "`quanlyduongsat`.`chuyen` (`id`, `thoi_gian_khoi_hanh`, `tau`, `tuyen`, `ngay_tao`,`nguoi_chinh_sua`) "
                    + "VALUES ('%s', '%s', '%s', '%s', '%s','%s')", n.id,n.ThoiGianKhoiHanh,n.Tau,n.Tuyen,n.NgayTao,"QL01");
            statement.executeUpdate(sql);
            conn.close();
            return true;
           // JOptionPane.showMessageDialog(null, "Thêm thành công!!!", "Thông báo", 1);
        } catch (Exception ex) {
            System.err.println(ex.getMessage());
        }
        return false;
    }

    public static boolean Xoa(String id) {
        try {
            Connection conn = DBConnect.Connect();
            String strSQL = "delete from chuyen where id = ?";
            PreparedStatement pst = conn.prepareStatement(strSQL);
            pst.setString(1, id);
            pst.executeUpdate();
            //JOptionPane.showMessageDialog(null, "Xóa thành công!!!", "Thông báo", 1);
            pst.close();
            return true;
        } catch (Exception ex) {
            ex.printStackTrace();
            ex.getMessage();
           // JOptionPane.showMessageDialog(null, ex.getStackTrace(), "Thông báo lỗi", JOptionPane.OK_OPTION);
        }
        return false;
    }
    
    public static ResultSet TimKiemTatCa() {
        try {
            Connection conn = DBConnect.Connect();
            String sql = "Select c.id,c.thoi_gian_khoi_hanh,t.ten_tau,c.tuyen,c.ngay_tao from chuyen c ,tau t where c.tau=t.id ";
            Statement st = conn.createStatement();
            ResultSet rs = st.executeQuery(sql);
          //  conn.close();
            return rs;

        } catch (Exception ex) {
            JOptionPane.showMessageDialog(null, ex.getStackTrace(), "Thông báo lỗi", JOptionPane.OK_OPTION);
            return null;
        }

    }
      public static boolean KiemTraTonTai(String MaChuyen)
    {
        try
        {
            Connection con = DBConnect.Connect();
            String sql = "Select * From Chuyen Where id like ?";
            
            PreparedStatement ps = con.prepareStatement(sql);
            ps.setString(1, MaChuyen);
            
            ResultSet rs = ps.executeQuery();
            
            if (rs.next())
                return true;
        }
        catch (Exception ex)
        {
        }
        return false;
    }
       public static boolean Update(ChuyenTauDTO C, String id)
    {
        try {       
            Connection con = DBConnect.Connect();
            String sql =  "Update chuyen set tau = ?, tuyen = ?, thoi_gian_khoi_hanh = ? ,id = ? Where id = ?";
            PreparedStatement ps = con.prepareStatement(sql);
            ps.setString(1, C.Tau.toString());
            ps.setString(2, C.Tuyen.toString());
            ps.setString(3, C.ThoiGianKhoiHanh.toString());
            ps.setString(4,C.id.toString());
            ps.setString(5,id);
            int n = ps.executeUpdate();
            con.close();
            
            if (n == 1)
                return true;
        } catch (Exception ex) {
           System.out.println(ex);
        }
        
    
        return false;
    }
}
